import os,shutil,tempfile,sys,subprocess
from pathlib import Path

folders_tree = {
    'anim 动画包': {},
    'exported 动画工程文件': {},
    'fx 特效材质': {},
    'images 图片': {
        'inventoryimages 库存物品图片': { 
            '_f':['prefab_id.tex','prefab_id.xml']
        }
    },
    'DETAILS 详细(给你自己看的)': {},
    'LSP 为lls提供智能识别支持': {
        'api 预设api': {},
        '_f':['...']
    },
    'scripts 脚本': {
        'components 组件': {},
        'core_%(modid)s': {
            'api 预设api': {
                '_f':['...']
            },
            'callers 调用器': {
                '_f':['...']
            },
            'data 数据库': {
                '_f':['...','tuning.lua MOD参数全局表']
            },
            'hooks 钩子': {
            },
            'languages 本地化': {
                '_f':['cn.lua 中文','en.lua 英文']
            },
            'ui': {},
            'utils 堆放自己的一些工具库(有待添加)': {},
            'widgets 构造UI的各种类': {},
        },
        'prefabs 预制物': {
            '_f': [
                '%(modid)s_module_dishes.lua 料理预制物模块',
                '%(modid)s_module_particle.lua 粒子预制物模块'
            ]
        },
    },
    'sound 音效': {},
    '_f': [
        'dst_mod-%(modid)s.code-workspace 工作区文件',
        'modmain.lua', 
        'modinfo.lua mod介绍配置'
    ]
}

def build_tree_string(folders_tree=folders_tree, prefix='', is_last=True, accumulator=''):
    keys = list(folders_tree.keys())
    for index, key in enumerate(keys):
        value = folders_tree[key]
        if key != '_f':  # 排除 '_f' 键
            if isinstance(value, dict):  # 如果值是字典，则递归处理
                line = prefix + ('└── ' if is_last and index == len(keys) - 1 else '├── ') + key + '\n'
                accumulator += line
                # 调整前缀以反映层级结构
                new_prefix = prefix + ('    ' if is_last and index == len(keys) - 1 else '│   ')
                accumulator = build_tree_string(value, new_prefix, key == keys[-1], accumulator)
            elif isinstance(value, list):  # 如果值是列表，则打印每个元素
                line = prefix + ('└── ' if is_last and index == len(keys) - 1 else '├── ') + key + '\n'
                accumulator += line
                new_prefix = prefix + ('    ' if is_last and index == len(keys) - 1 else '│   ')
                for file_index, file in enumerate(value):
                    is_last_file = (file_index == len(value) - 1)
                    line = new_prefix + ('└── ' if is_last_file else '├── ') + file + '\n'
                    accumulator += line
        
        # 处理 '_f' 键
        if key == '_f':
            for file_index, file in enumerate(value):
                is_last_file = (file_index == len(value) - 1)
                line = prefix + ('└── ' if is_last_file else '├── ') + file + '\n'
                accumulator += line

    return accumulator